package cn.tedu.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.tedu.service.UserService;
import cn.tedu.service.impl.UserServiceImpl;

/**
 * Servlet implementation class CheckNameServlet
 */
public class CheckNameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckNameServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.获取数据,用户在注册的时候填入的用户名
		String uname=request.getParameter("username");
		//2.调用查询用户名是否存在的业务
		UserService userService=new UserServiceImpl();
		boolean flag=userService.findUserByName(uname);
		//根据业务的返回结果做跳转/响应
		String responseText="";
		if(flag) {
			responseText="用户名被占用";
		}else {
			responseText="用户名可以使用";
		}
		//把文本响应给客户端浏览器
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out=response.getWriter();
		//用字符流,把带有UTF-8编码的字符串发送给客户端
		out.println(responseText);
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
